﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using HalalCheck.CustomViews;
using HalalCheck.DataModels;
using HalalCheck.ViewModels;
using SpotOffer.Utils;
using Xamarin;
using Xamarin.Forms;
using XLabs.Forms.Mvvm;

namespace HalalCheck.View
{
    public class RootPage : MasterDetailPage
    {
        public static MenuPage _menu;
        public static RootNav Nav { get; set; }

        public RootPage()
        {
            _menu = (ViewFactory.CreatePage<MenuViewModel, Page>() as MenuPage);
            _menu.MenuListView.ItemSelected += (sender, e) =>
            {
                if (e.SelectedItem != null)
                {
                    NavigateTo(e.SelectedItem as MenuItemHalalCheck);
                    ((ListView)sender).SelectedItem = null; // de-select the row
                }

            };

            Master = _menu;
            _menu.MenuListView.SelectedItem = _menu.MenuListView.ItemsSource.Cast<MenuItemHalalCheck>().First();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            IsPresented = true;
        }

        private async void NavigateTo(MenuItemHalalCheck menu)
        {
            Insights.Track("MenuClicked", new Dictionary<string, string> { { "MenuClicked", menu.ViewModelType.Name }, });
            Page displayPage = (Page)ViewFactory.CreatePage(menu.ViewModelType);
            if (Nav == null)
            {

                Nav = AppViews.ReturnNavigationPage(displayPage, menu.Title);

                Detail = Nav;
            }
            else
            {
               Nav.SwapRoot(displayPage, menu.Title);
            }

            IsPresented = false;
        }

    }
}